//
//  OneViewController.m
//  项目二
//
//  Created by rimi on 16/5/13.
//  Copyright © 2016年 objective. All rights reserved.
//

#import "OneViewController.h"

@interface OneViewController ()<UIPickerViewDelegate,UIPickerViewDataSource>

/**文本框*/
@property(nonatomic,strong)UITextField *textField;
/**按钮*/
@property(nonatomic,strong)UIButton *button;
/**按钮*/
@property(nonatomic,strong)UIButton *button1;
/**UIPickerView*/
@property(nonatomic,strong)UIPickerView *pickerView;
/**数组*/
@property(nonatomic,strong)NSMutableArray *array;
/**字典*/
@property(nonatomic,strong)NSDictionary *dictionary;
@end

@implementation OneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"UIPickerView";
    self.view.backgroundColor = [UIColor whiteColor];
    [self initDataSource];
    [self initIdenface];
}

/**初始化数据源*/
-(void)initDataSource {
    _array = [NSMutableArray arrayWithObjects:@"中国",@"美国", nil];
    
    _dictionary = @{@"中国":@[@"深圳",@"成都",@"上海",@"北京",@"广州"],
                    @"美国":@[@"洛杉矶",@"费城",@"夏威夷",@"艾欧里亚",@"班德尔城"]};
   

}

-(void)initIdenface {
    [self.view addSubview:self.pickerView];
    [self.view addSubview:self.textField];
    [self.view addSubview:self.button];
    [self.view addSubview:self.button1];
}
#pragma mark - Get Method
- (UIPickerView *)pickerView{
    if (!_pickerView) {
        _pickerView = [[UIPickerView alloc]initWithFrame:CRM(0, CGRectGetMaxY(self.view.bounds) - 200, CGRectGetWidth(self.view.bounds), 100)];
        /**添加代理*/
        _pickerView.delegate = self;
        _pickerView.dataSource = self;
        _pickerView.hidden = YES;
    }
    return _pickerView;
}

-(UITextField *)textField{
    if (!_textField) {
        _textField = [[UITextField alloc]initWithFrame:CGRectMake(90, 200  , 200, 30)];
        _textField.borderStyle = UITextBorderStyleLine;
        _textField.textAlignment = NSTextAlignmentCenter;
    }
    return _textField;
}

-(UIButton *)button{
    if (!_button) {
        _button = [UIButton buttonWithType:UIButtonTypeCustom];
        _button.frame = CRM(90, 240, 200, 30);
        _button.backgroundColor = [UIColor redColor];
        [_button setTitle:@"请点击选择城市" forState:UIControlStateNormal];
        _button.tintColor = [UIColor redColor];
        _button.layer.borderColor = [UIColor orangeColor].CGColor;
        [_button addTarget:self action:@selector(buttonTarge:) forControlEvents:UIControlEventTouchUpInside];
        _button1.layer.borderWidth = 1.0;
    }
    return _button;
}

-(UIButton *)button1{
    if (!_button1) {
        _button1 = [UIButton buttonWithType:UIButtonTypeCustom];
        _button1.frame = CRM(160, 280, 50, 30);
        _button1.backgroundColor = [UIColor redColor];
        _button1.tintColor = [UIColor blueColor];
        _button1.layer.borderColor = [UIColor orangeColor].CGColor;
        [_button1 setTitle:@"确定" forState:UIControlStateNormal];
        [_button1 addTarget:self action:@selector(button1Targe:) forControlEvents:UIControlEventTouchUpInside];
        _button1.layer.borderWidth = 1.0;
    }
    return _button1;
}
#pragma mark - Method
-(void)buttonTarge:(UIButton *)sender{

    _pickerView.hidden = NO;
}

-(void)button1Targe:(UIButton *)sender{
    _pickerView.hidden = YES;
    NSString *string = _textField.text;
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"你选择的城市" message:string preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:alertAction];
    [self presentViewController:alertController animated:YES completion:nil];
}
/**设置列数*/
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return _array.count;
}

/**设置行数*/
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component == 0) {
        return _array.count;
    }else{
       
        NSInteger rowCountry = [pickerView selectedRowInComponent:0];
        NSString *countryName = _array[rowCountry];
        NSArray *citys = _dictionary[countryName];
        return  citys.count;
    }

}

/**设置行高*/
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 20;
}

/**设置行宽*/
-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
    return self.view.frame.size.width / 3;
}

/**设置每行显示内容*/
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (component == 0) {
        return _array[row];
    }else{
        NSInteger rowCountry = [pickerView selectedRowInComponent:0];
        NSString *countryName = _array[rowCountry];
        NSArray *citys = _dictionary[countryName];
        return citys[row];
    }
}

/**被选中行*/
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{

    NSInteger countryRowNumber = [pickerView selectedRowInComponent:0];
    NSInteger cityRowNumber = [pickerView selectedRowInComponent:1];
    NSString *countryName = _array[countryRowNumber];
    NSArray *citysArray = _dictionary[countryName];
    NSString *cityName = citysArray[cityRowNumber];
    NSString *text = [countryName stringByAppendingString:cityName];
    _textField.text = text;
    //刷新所有列
    [_pickerView reloadAllComponents];
}
@end
